package Action;

import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ModelDriven;

import Dao.UserDao;
import DaoImpl.UserDaoImpl;
import Entity.User;
import Service.UserService;

public class UserAction extends SuperAction implements ModelDriven<User>{
	
	private static final long serialVersionUID = 1L;
	private User user = new User();
	
	@SkipValidation
	public String queryById() {
		UserService userService=new UserService();
		String uid = request.getParameter("userId");
		String type = request.getParameter("type");
		User u = userService.findInformation(uid);
		session.setAttribute("user", u);
		if ("userInfo".equals(type)) {
			return "queryUsersInfo_success";
		}
		return "queryById_success";
	}
	
	public String modify() {
		return null;
	}
	
	public void validate()
    {
     	if("".equals(user.getStudentId().trim()))
     	{
     		this.addFieldError("usernameError","请填写用户名");
     	}
     	if(user.getStupassword().length()<6)
     	{
     		this.addFieldError("passwordError","密码小于6位");
     	}
     		
    }
	@SkipValidation
	public String logout()
	{
		if (session.getAttribute("loginUserName") != null)
		{
			session.removeAttribute("loginUserName");
		}
		return "logout_success";
	}
	public String login() {
		System.out.println("login");
		UserDao udao = new UserDaoImpl();
		
		User u1 = udao.login(user);
		if (u1 != null) {
			session.setAttribute("loginUser", u1);
			System.out.println("login1");
			return "loginUser_success";
		}
		else {
			System.out.println("u1 == null");
			return "loginUser_failure";
		}
		
	}
	
	public User getModel() {
		return this.user;
	}

}
